package adminUserAccountService

import (
	"encoding/json"

	"gitee.com/krio/ginc/example/core/exerr"
	"gitee.com/krio/ginc/example/core/lib/conf"
	exampleDtoApiPbAdminUserAccount "gitee.com/krio/ginc/example/dto/api/pb/admin/user/account"
	adminUserAccountDao "gitee.com/krio/ginc/example/internal/admin/user/account/dao"
	adminUserAccountModel "gitee.com/krio/ginc/example/internal/admin/user/account/model"
	commonEntry "gitee.com/krio/ginc/example/internal/common/entry/common"
	commonModel "gitee.com/krio/ginc/example/internal/common/model/common"
	uuidThor "gitee.com/krio/ginc/example/util/thor/uuid"
	userUtil "gitee.com/krio/ginc/example/util/user"
	"github.com/gin-gonic/gin"
)

// AdminUserAccountList 列表
func AdminUserAccountList(ctx *gin.Context, req *exampleDtoApiPbAdminUserAccount.AdminUserAccountListReq) (*exampleDtoApiPbAdminUserAccount.AdminUserAccountListResp, error) {
	resp := &exampleDtoApiPbAdminUserAccount.AdminUserAccountListResp{}
	search := struct {
		Keyword string `json:"keyword"`
	}{}
	if req.Keywords != "" {
		err := json.Unmarshal([]byte(req.Keywords), &search)
		if err != nil {
			return nil, err
		}
	}
	dao := &adminUserAccountDao.AdminUserAccountDao{}
	list, total, err := dao.UserList(ctx, &commonEntry.ListCommonReq{
		Page:    req.Page,
		Limit:   req.Limit,
		Keyword: search.Keyword,
		OrderBy: []*commonEntry.OrderBy{
			{
				IsDesc: true,
				Field:  "id",
			},
		},
	})
	if err != nil {
		return nil, err
	}
	var data []*exampleDtoApiPbAdminUserAccount.AdminUserAccountInfo
	for _, v := range list {
		data = append(data, formatDetail(v))
	}
	resp.Total = total
	resp.List = data
	return resp, nil
}

// AdminUserAccountDetail 详情
func AdminUserAccountDetail(ctx *gin.Context, req *exampleDtoApiPbAdminUserAccount.AdminUserAccountDetailReq) (*exampleDtoApiPbAdminUserAccount.AdminUserAccountInfo, error) {
	dao := &adminUserAccountDao.AdminUserAccountDao{}
	detail, has, err := dao.UserDetail(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, exerr.DataNotFound
	}

	return formatDetail(detail), nil
}

// AdminUserAccountAdd 新增
func AdminUserAccountAdd(ctx *gin.Context, req *exampleDtoApiPbAdminUserAccount.AdminUserAccountInfo) (*exampleDtoApiPbAdminUserAccount.AdminUserAccountAddResp, error) {
	pwd, err := userUtil.EncryptPWD(req.UserPwd)
	if err != nil {
		return nil, err
	}
	userId, err := uuidThor.GetNumberUUID(int64(conf.C.App.MachineID))
	if err != nil {
		return nil, err
	}
	one := &adminUserAccountModel.AdminUserAccount{
		UserName:    req.UserName,
		UserAccount: req.UserAccount,
		UserUuid:    userId,
		UserPwd:     string(pwd),
		Status:      commonModel.DataStatus(req.Status),
	}
	dao := &adminUserAccountDao.AdminUserAccountDao{}
	id, err := dao.UserAdd(ctx, one)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminUserAccount.AdminUserAccountAddResp{
		Id: id,
	}, nil
}

// AdminUserAccountUpdate 修改
func AdminUserAccountUpdate(ctx *gin.Context, req *exampleDtoApiPbAdminUserAccount.AdminUserAccountInfo) (*exampleDtoApiPbAdminUserAccount.AdminUserAccountAddResp, error) {
	var (
		pwd []byte
		err error
	)
	if req.UserPwd != "" {
		pwd, err = userUtil.EncryptPWD(req.UserPwd)
		if err != nil {
			return nil, err
		}
	}
	dao := &adminUserAccountDao.AdminUserAccountDao{}
	_, err = dao.UserUpdate(ctx, &adminUserAccountModel.AdminUserAccount{
		Basic:       commonModel.Basic{ID: req.Id},
		UserName:    req.UserName,
		UserAccount: req.UserAccount,
		UserPwd:     string(pwd),
		Status:      commonModel.DataStatus(req.Status),
	})
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminUserAccount.AdminUserAccountAddResp{}, nil
}

// AdminUserAccountDelete 删除
func AdminUserAccountDelete(ctx *gin.Context, req *exampleDtoApiPbAdminUserAccount.AdminUserAccountDeleteReq) (*exampleDtoApiPbAdminUserAccount.AdminUserAccountDeleteResp, error) {
	dao := &adminUserAccountDao.AdminUserAccountDao{}
	err := dao.UserDelete(ctx, req.Ids)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminUserAccount.AdminUserAccountDeleteResp{}, nil
}

// 数据装换
func formatDetail(v *adminUserAccountModel.AdminUserAccount) *exampleDtoApiPbAdminUserAccount.AdminUserAccountInfo {
	return &exampleDtoApiPbAdminUserAccount.AdminUserAccountInfo{
		Id:          v.ID,
		UserName:    v.UserName,
		UserAccount: v.UserAccount,
		UserUuid:    v.UserUuid,
		Status:      int32(v.Status),
		CreateTime:  v.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdateTime:  v.UpdatedAt.Format("2006-01-02 15:04:05"),
	}
}
